package com.jiliang.auth.service.impl;


import cn.hutool.extra.pinyin.PinyinUtil;
import com.jiliang.auth.dao.mapper.AuthRoleFuncExMapper;
import com.jiliang.auth.dao.mapper.AuthRoleMapper;
import com.jiliang.auth.dao.mapper.AuthRoleMemberMapper;
import com.jiliang.auth.dao.model.AuthRole;
import com.jiliang.auth.dao.model.AuthRoleExample;
import com.jiliang.auth.dao.model.AuthRoleMember;
import com.jiliang.auth.dao.model.AuthRoleMemberExample;
import com.jiliang.auth.domain.AuthRoleBo;
import com.jiliang.auth.domain.AuthRoleMemberBo;
import com.jiliang.auth.service.AuthRoleMemberService;
import com.jiliang.auth.service.AuthRoleService;
import com.jiliang.common.bean.BeanCopierDsUtil;
import com.jiliang.common.constant.CommonDsConstant;
import com.jiliang.common.utils.PinyinConverter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限角色管理
 *
 * @author 
 * @version 1.0
 * @Date 8/14 9:47
 */
@Service
public class AuthRoleServiceImpl implements AuthRoleService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AuthRoleMapper authRoleMapper;
    @Resource
    private AuthRoleMemberService authRoleMemberService;

    @Autowired
    private AuthRoleMemberMapper authRoleMemberMapper;

    @Autowired
    private AuthRoleFuncExMapper authRoleFuncExMapper;

    /**
     * 根据条件查询数据集合
     *
     * @param
     * @param authRoleBo
     * @return java.util.List<com.nxin.middle.jiliang.auth.dao.model.AuthRole>
     * @author 
     * @date 17:23 8/16
     **/
    @Override
    public List<AuthRole> selListByParam(AuthRoleBo authRoleBo) {
        log.info("==========查询条件==========" + authRoleBo);
        if (authRoleBo == null) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
        }
        AuthRoleExample example = new AuthRoleExample();
        AuthRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED_VAL);
        if (Strings.isNotBlank(authRoleBo.getRoleCode())) {
            criteria.andRoleCodeEqualTo(authRoleBo.getRoleCode());
        }
        if (CollectionUtils.isNotEmpty(authRoleBo.getRoleCodeList())) {
            criteria.andRoleCodeIn(authRoleBo.getRoleCodeList());
        }

        if (Strings.isNotBlank(authRoleBo.getName())) {
            criteria.andNameLike("%"+authRoleBo.getName()+"%");
            // criteria.andNameEqualTo(authRoleBo.getName());
        }
        if (authRoleBo.getCanVisit() != null) {
            criteria.andCanVisitEqualTo(authRoleBo.getCanVisit());
        }
        if (authRoleBo.getSubSystemId() != null) {
            criteria.andSubSystemIdEqualTo(authRoleBo.getSubSystemId());
        }
        if (Strings.isNotBlank(authRoleBo.getSubSystemIds())) {
            String[] subSystemIds = authRoleBo.getSubSystemIds().split(CommonDsConstant.DELIMITER_SG_ZY);
            List<Short> subSystemIdList = new ArrayList<>();
            for (String subSystemId : subSystemIds) {
                subSystemIdList.add(Short.parseShort(subSystemId));
            }
            criteria.andSubSystemIdIn(subSystemIdList);
        }
        if (authRoleBo.getSystemId() != null) {
            criteria.andSystemIdEqualTo(authRoleBo.getSystemId());
        }
        example.setOrderByClause("create_time asc");
        List<AuthRole> authRoleList = authRoleMapper.selectByExample(example);
        return authRoleList;
    }

    /**
     * 根据主键查询
     *
     * @param roleCode
     * @return com.nxin.middle.jiliang.auth.dao.model.AuthRole
     * @author 
     * @date 11:19 8/14
     **/
    @Override
    public AuthRole selAuthRoleByCode(String roleCode) {
        log.info("==========查询code==========" + roleCode);
        if (Strings.isBlank(roleCode)) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
        }
        AuthRole authRole = authRoleMapper.selectByPrimaryKey(roleCode);
        return authRole;
    }

    /**
     * 根据主键修改权限角色信息
     *
     * @param authRole
     * @return int
     * @author 
     * @date 11:12 8/14
     **/
    @Override
    public int updAuthRoleByCode(AuthRole authRole) {
        log.info("==========修改信息==========" + authRole);
        if (authRole == null || Strings.isBlank(authRole.getRoleCode())) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
        }
        int result = authRoleMapper.updateByPrimaryKeySelective(authRole);
        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_UPDATE_ERROR);
        }
        return result;
    }

    /**
     * 根据主键逻辑删除权限角色信息
     *
     * @param roleCode
     * @return int
     * @author 
     * @date 10:11 8/14
     **/
    @Override
    public int delAuthRoleByCode(String roleCode) {
        log.info("==========删除主键==========" + roleCode);
        if (Strings.isBlank(roleCode)) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
        }
        AuthRole authRole = new AuthRole();
        authRole.setRoleCode(roleCode);
        authRole.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED_VAL);
        int result = authRoleMapper.updateByPrimaryKeySelective(authRole);
        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_DELETE_ERROR);
        }
        return result;
    }

    /**
     * 添加权限角色
     *
     * @param authRole
     * @return void
     * @author 
     * @date 9:58 8/14
     **/
    @Override
    public String addAuthRole(AuthRole authRole) {
        log.info("==========添加权限角色==========" + authRole);
        String roleCode = PinyinConverter.toPinyin(PinyinUtil.getPinyin(authRole.getName()));
        if (authRole == null
                || Strings.isBlank(authRole.getRoleCode())) {
//            throw new ValidationException(Errors.SYSTEM_REQUEST_PARAM_ERROR);
        }
        AuthRole role = selAuthRoleByCode(roleCode);
        if (role != null) {
            log.info("==========已存在，修改==========");
            authRole.setIsEnabled(CommonDsConstant.IS_ENABLED_VAL);
            updAuthRoleByCode(authRole);
            return roleCode;
        }

        authRole.setRoleCode(roleCode);
        int result = authRoleMapper.insertSelective(authRole);
        if (result <= 0) {
//            throw new ServerServiceException(Errors.SYSTEM_INSERT_ERROR);
        }
        return roleCode;
    }

    @Override
    public List<AuthRoleBo> selRoleListBySysId(AuthRoleBo authRoleBo) {
        List<AuthRole> authRoleList = selListByParam(authRoleBo);
        if (CollectionUtils.isEmpty(authRoleList)) {
            return null;
        }
        List<AuthRoleBo> authRoleBoList = BeanCopierDsUtil.copyPropertiesOfList(authRoleList, AuthRoleBo.class);
        if (Strings.isBlank(authRoleBo.getBusType()) || Strings.isBlank(authRoleBo.getBusId()) || authRoleBo.getBoId() == null) {
            return authRoleBoList;
        }
        for (AuthRoleBo bo : authRoleBoList) {
            AuthRoleMemberBo param = new AuthRoleMemberBo();
            param.setSystemId(bo.getSystemId());
            param.setSubSystemId(bo.getSubSystemId());
            param.setRoleCode(bo.getRoleCode());
            param.setBusType(authRoleBo.getBusType());
            param.setBusId(authRoleBo.getBusId());
            param.setBoId(authRoleBo.getBoId());
            List<AuthRoleMemberBo> roleMemberBoList = authRoleMemberService.selListBySysIdSubSysId(param);
            if (!CollectionUtils.isEmpty(roleMemberBoList)) {
                bo.setIsChecked(CommonDsConstant.DEFAULT_INT_YES);
            } else {
                bo.setIsChecked(CommonDsConstant.DEFAULT_INT_NO);
            }
        }
        return authRoleBoList;
    }

    @Override
    public int countByRoleCode(String roleCode) {
        AuthRoleMemberExample example = new AuthRoleMemberExample();
        AuthRoleMemberExample.Criteria criteria = example.createCriteria();
        criteria.andRoleCodeEqualTo(roleCode);
        List<AuthRoleMember> authRoleMembers = authRoleMemberMapper.selectByExample(example);
        return authRoleMembers.size();
    }

    /**
     * 根据角色代码查询功能列表
     * @param roleCode
     * @return
     */
    @Override
    public List<String> selFuncListByRoleCode(String roleCode) {
        List<String> funcList = authRoleFuncExMapper.selectFuncListByRoleCode(roleCode);
        return funcList;
    }
}
